package cn.iocoder.yudao.module.polling.controller.admin.pollingstandard;

import cn.iocoder.yudao.module.polling.convert.ids.OrderUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.polling.controller.admin.pollingstandard.vo.*;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingstandard.PollingStandardDO;
import cn.iocoder.yudao.module.polling.convert.pollingstandard.PollingStandardConvert;
import cn.iocoder.yudao.module.polling.service.pollingstandard.PollingStandardService;

@Api(tags = "管理后台 - 巡检标准")
@RestController
@RequestMapping("/polling/standard")
@Validated
public class PollingStandardController {

    @Resource
    private PollingStandardService standardService;

    @PostMapping("/create")
    @ApiOperation("创建巡检标准")
    @PreAuthorize("@ss.hasPermission('polling:standard:create')")
    public CommonResult<Integer> createStandard(@Valid @RequestBody PollingStandardAddVo createReqVO) {
        createReqVO.setPollingCode(OrderUtil.getId("XJBZ"));
        return success(standardService.createStandard(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检标准")
    @PreAuthorize("@ss.hasPermission('polling:standard:update')")
    public CommonResult<Boolean> updateStandard(@Valid @RequestBody PollingStandardUpdateReqVO updateReqVO) {
        standardService.updateStandard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('polling:standard:delete')")
    public CommonResult<Boolean> deleteStandard(@RequestParam("id") Integer id) {
        standardService.deleteStandard(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('polling:standard:query')")
    public CommonResult<PollingStandardRespVO> getStandard(@RequestParam("id") Integer id) {
        PollingStandardDO standard = standardService.getStandard(id);
        //PollingStandardConvert.INSTANCE.convert(standard)
        PollingStandardRespVO pollingStandardRespVO=new PollingStandardRespVO();
        BeanUtils.copyProperties(standard,pollingStandardRespVO);
        return success(pollingStandardRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得巡检标准列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('polling:standard:query')")
    public CommonResult<List<PollingStandardRespVO>> getStandardList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingStandardDO> list = standardService.getStandardList(ids);
        return success(PollingStandardConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检标准分页")
    @PreAuthorize("@ss.hasPermission('polling:standard:query')")
    public CommonResult<PageResult<PollingStandardRespVO>> getStandardPage(@Valid PollingStandardPageReqVO pageVO) {
        PageResult<PollingStandardDO> pageResult = standardService.getStandardPage(pageVO);
       return success(PollingStandardConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/my")
    @ApiOperation("获得巡检标准")
    @PreAuthorize("@ss.hasPermission('polling:standard:myquery')")
    public CommonResult<PageResult<PollingStandardQueryVo>> getStandardPageMy(@Valid PollingStandardQueryVo pageVO) {
        List<PollingStandardQueryVo> list = standardService.selectPollingStandardMList(pageVO);
        PageResult<PollingStandardQueryVo> pageResult=new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出巡检标准 Excel")
    @PreAuthorize("@ss.hasPermission('polling:standard:export')")
    @OperateLog(type = EXPORT)
    public void exportStandardExcel(@Valid PollingStandardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PollingStandardDO> list = standardService.getStandardList(exportReqVO);
        // 导出 Excel
        List<PollingStandardExcelVO> datas = PollingStandardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检标准.xls", "数据", PollingStandardExcelVO.class, datas);
    }

}
